﻿using System;
using System.Windows.Input;
using System.Windows.Threading;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;

namespace Songhay.Silverlight.BiggestBox.AdditionalSamplesPart.ViewModels
{
    using Models;
    using Songhay.Mvvm.Models;
    using Songhay.Mvvm.ViewModels;

    /// <summary>
    /// View Model for Telerik Radial Gauge Sample
    /// </summary>
    public class TelerikRadialGaugeViewModel : ViewModelBase
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="TelerikRadialGaugeViewModel"/> class.
        /// </summary>
        /// <remarks>
        /// For details, see
        /// “How to: Implement Property Change Notification”
        /// http://msdn.microsoft.com/en-us/library/ms743695.aspx
        /// A sample <c>set</c> block:
        /// <code>
        /// set
        /// {
        /// _myProperty = value;
        /// OnPropertyChanged("MyProperty");
        /// }
        /// </code>
        /// </remarks>
        public TelerikRadialGaugeViewModel()
        {
            if(this.IsInDesignMode) return;

            this._emulator = new TelerikRealDataEmulator();
            var timer = new DispatcherTimer
            {
                Interval = new TimeSpan(0, 0, 5)
            };
            timer.Tick += (s, args) =>
            {
                var message = new LightMessage<double>(this._emulator.GetNextValue())
                {
                    Source = LightMessageSource.ViewModel,
                    UniqueId = "TelerikRadialGaugeSample"
                };
                Messenger.Default.Send(message);
            };

            #region commanding:

            this.ViewFocusCommand = new RelayCommand(
            () =>
            {
                timer.Start();
            });

            this.ViewLostFocusCommand = new RelayCommand(
            () =>
            {
                timer.Stop();
            });

            #endregion
        }

        /// <summary>
        /// Gets or sets the view focus command.
        /// </summary>
        /// <value>The view focus command.</value>
        public ICommand ViewFocusCommand { get; private set; }

        /// <summary>
        /// Gets or sets the view lost focus command.
        /// </summary>
        /// <value>The view lost focus command.</value>
        public ICommand ViewLostFocusCommand { get; private set; }

        TelerikRealDataEmulator _emulator;
    }
}